package com.yaoweigroup.community.ui.my;

import java.util.Iterator;
import java.util.Map;

import org.apache.http.entity.StringEntity;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.json.JSONObject;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.yaoweigroup.community.R;
import com.yaoweigroup.community.config.Common;
import com.yaoweigroup.community.config.Constant;
import com.yaoweigroup.community.config.Urls;
import com.yaoweigroup.community.enity.Check;
import com.yaoweigroup.community.enity.Pressure;
import com.yaoweigroup.community.enity.UserInfo;
import com.yaoweigroup.community.util.DialogUtil;
import com.yaoweigroup.community.util.JsonUtil;
import com.yaoweigroup.community.util.LogUtil;
import com.yaoweigroup.community.util.LoginUtil;
import com.yaoweigroup.community.util.SharePreferenceUtil;
import com.yaoweigroup.community.util.UtilHttp;

public class MyHealthActivity extends Activity implements OnClickListener {

	private RelativeLayout report_rl;
	private RelativeLayout left_rl;
	private RelativeLayout record_rl;
	private ImageView report_iv;
	private ImageView record_iv;

	private LinearLayout my_check_ll;
	private LinearLayout my_pressure_ll;
	private SharePreferenceUtil util;
	private ObjectMapper mapper;
	private Dialog waitDialog;
	private Check check;

	private TextView bmd_t_tv;
	private TextView bmd_z_tv;
	private TextView bmi_tv;
	private TextView bmr_tv;
	private TextView bo_tv;
	private TextView chol_tv;
	private TextView dbp_tv;
	private TextView fat_tv;
	private TextView fetalheart_tv;
	private TextView glu_tv;
	private TextView height_tv;
	private TextView hipline_tv;
	private TextView pulse_tv;
	private TextView sbp_tv;
	private TextView ua_tv;
	private TextView waistline_tv;
	private TextView water_tv;
	private TextView weight_tv;
	private TextView whr_tv;

	private TextView checkdate_tv;
	private TextView functioncode_tv;
	private TextView cardNo_tv;
	private TextView sbp_p_tv;
	private TextView dbp_p_tv;
	private TextView pulse_p_tv;
	private TextView icpcode_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_health);

		checkdate_tv = (TextView) findViewById(R.id.checkdate_tv);
		functioncode_tv = (TextView) findViewById(R.id.functioncode_tv);
		cardNo_tv = (TextView) findViewById(R.id.cardNo_tv);
		sbp_p_tv = (TextView) findViewById(R.id.sbp_p_tv);
		dbp_p_tv = (TextView) findViewById(R.id.dbp_p_tv);
		pulse_p_tv = (TextView) findViewById(R.id.pulse_p_tv);
		icpcode_tv = (TextView) findViewById(R.id.icpcode_tv);

		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		waitDialog = DialogUtil.createWaitDialog(this, "数据获取中..");
		mapper = new ObjectMapper();
		util = new SharePreferenceUtil(this, Constant.prf);
		my_check_ll = (LinearLayout) findViewById(R.id.my_check_ll);
		my_pressure_ll = (LinearLayout) findViewById(R.id.my_pressure_ll);
		report_rl = (RelativeLayout) findViewById(R.id.report_rl);
		report_rl.setOnClickListener(this);
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);
		record_rl = (RelativeLayout) findViewById(R.id.record_rl);
		record_rl.setOnClickListener(this);

		report_iv = (ImageView) findViewById(R.id.report_iv);
		record_iv = (ImageView) findViewById(R.id.record_iv);

		bmd_t_tv = (TextView) findViewById(R.id.bmd_t_tv);
		bmd_z_tv = (TextView) findViewById(R.id.bmd_z_tv);
		bmi_tv = (TextView) findViewById(R.id.bmi_tv);
		bmr_tv = (TextView) findViewById(R.id.bmr_tv);
		bo_tv = (TextView) findViewById(R.id.bo_tv);
		chol_tv = (TextView) findViewById(R.id.chol_tv);
		dbp_tv = (TextView) findViewById(R.id.dbp_tv);
		fat_tv = (TextView) findViewById(R.id.fat_tv);
		fetalheart_tv = (TextView) findViewById(R.id.fetalheart_tv);
		glu_tv = (TextView) findViewById(R.id.glu_tv);
		height_tv = (TextView) findViewById(R.id.height_tv);
		hipline_tv = (TextView) findViewById(R.id.hipline_tv);
		pulse_tv = (TextView) findViewById(R.id.pulse_tv);
		sbp_tv = (TextView) findViewById(R.id.sbp_tv);
		ua_tv = (TextView) findViewById(R.id.ua_tv);
		waistline_tv = (TextView) findViewById(R.id.waistline_tv);
		water_tv = (TextView) findViewById(R.id.water_tv);
		weight_tv = (TextView) findViewById(R.id.weight_tv);
		whr_tv = (TextView) findViewById(R.id.whr_tv);
		obtainData();

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		case R.id.report_rl:
			my_check_ll.setVisibility(View.VISIBLE);
			my_pressure_ll.setVisibility(View.GONE);
			report_iv.setVisibility(View.VISIBLE);
			record_iv.setVisibility(View.GONE);
			break;
		case R.id.record_rl:
			report_iv.setVisibility(View.GONE);
			record_iv.setVisibility(View.VISIBLE);
			my_check_ll.setVisibility(View.GONE);
			my_pressure_ll.setVisibility(View.VISIBLE);
			break;
		}
	}

	private String uid = "6";
	private String cardNo = "46546";
	public final int NEED_LOGIN = 0x2210;
	public final int DATA_SUCCESS = NEED_LOGIN + 1;
	public final int DATA_FAIL = NEED_LOGIN + 2;
	public final int PRESSUCE_SUCCESS = NEED_LOGIN + 3;
	public final int PRESSUCE_FAIL = NEED_LOGIN + 4;

	private void obtainData() {
		UserInfo user = (UserInfo) util.loadObject("user");
		if (user != null) {
			uid = user.getUid();
			cardNo = user.getMediano();
		}
		waitDialog.show();
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		LogUtil.d("wxf", jsonUtil.toString());
		jsonUtil.add("Uid", uid);
		// jsonUtil.add("cardNo", cardNo);
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getHealthCabinLately,
				params, new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						String result = responseInfo.result;
						try {
							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 10000) {
								handler.sendEmptyMessage(NEED_LOGIN);
							} else if (state == 200) {

								check = mapper.readValue(obj.getString("data"),
										new TypeReference<Check>() {
										});
								handler.sendEmptyMessage(DATA_SUCCESS);
							} else {
								handler.sendEmptyMessage(DATA_FAIL);
							}

						} catch (Exception e) {
							handler.sendEmptyMessage(DATA_FAIL);
							LogUtil.e("wxf", e.toString());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(DATA_FAIL);

					}

				});
	}

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case NEED_LOGIN:
				LoginUtil
						.isLoginSuccess(MyHealthActivity.this, handler, mapper);
				break;
			case DATA_FAIL:
				waitDialog.dismiss();
				break;
			case DATA_SUCCESS:
				waitDialog.dismiss();
				obtainPressure();
				refreshCheck();
				break;
			case LoginUtil.LOGIN_SUCCESS:
				obtainData();

				break;
			case LoginUtil.LOGIN_FAIL:
				waitDialog.dismiss();
				break;
			case PRESSUCE_SUCCESS:
				refreshPressure();
				break;
			case PRESSUCE_FAIL:

				break;
			}
		};
	};

	private void refreshCheck() {
		if (check == null) {
			return;
		}
		bmd_t_tv.setText(check.getBmd_t() + "");
		bmd_z_tv.setText(check.getBmd_z() + "");
		bmi_tv.setText(check.getBmi() + "");
		bmr_tv.setText(check.getBmr() + "");
		bo_tv.setText(check.getBo() + "");
		chol_tv.setText(check.getChol() + "");
		dbp_tv.setText(check.getDbp() + "");
		fat_tv.setText(check.getFat() + "");
		fetalheart_tv.setText(check.getFetalheart() + "");
		glu_tv.setText(check.getGlu() + "");
		height_tv.setText(check.getHeight() + "");
		hipline_tv.setText(check.getHipline() + "");
		pulse_tv.setText(check.getPulse() + "");
		sbp_tv.setText(check.getSbp() + "");
		ua_tv.setText(check.getUa() + "");
		waistline_tv.setText(check.getWaistline() + "");
		water_tv.setText(check.getWater() + "");
		weight_tv.setText(check.getWeight() + "");
		whr_tv.setText(check.getWhr() + "");
	}

	private Pressure pressure;

	private void obtainPressure() {
		UserInfo user = (UserInfo) util.loadObject("user");
		if (user != null) {
			uid = user.getUid();
			cardNo = user.getMediano();
		}
		HttpUtils http = UtilHttp.getHttpUtils(this);
		RequestParams params = new RequestParams();
		JsonUtil jsonUtil = new JsonUtil();
		jsonUtil.add("Uid", uid);
		//jsonUtil.add("cardNo", cardNo);
		LogUtil.d("wxf", jsonUtil.toString());
		StringEntity enity = null;
		try {
			enity = new StringEntity(jsonUtil.toString());
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}
		params.setContentType("application/json");
		params.setBodyEntity(enity);
		params.setHeader("Accept", "application/json");
		http.send(HttpRequest.HttpMethod.POST, Urls.getLateMonthBloodPressure,
				params, new RequestCallBack<String>() {

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						LogUtil.d("wxf", "result = " + responseInfo.result);
						String result = responseInfo.result;
						try {
							JSONObject obj = new JSONObject(result);
							int state = obj.getInt("state");
							if (state == 200) {
								pressure = mapper.readValue(
										obj.getString("data"),
										new TypeReference<Pressure>() {
										});
								handler.sendEmptyMessage(PRESSUCE_SUCCESS);
							} else {
								handler.sendEmptyMessage(PRESSUCE_FAIL);
							}
						} catch (Exception e) {
							handler.sendEmptyMessage(PRESSUCE_FAIL);

							LogUtil.e("wxf", e.toString());
						}

					}

					@Override
					public void onFailure(HttpException error, String msg) {
						LogUtil.e("wxf", error.toString());
						handler.sendEmptyMessage(PRESSUCE_FAIL);

					}

				});
	}

	private void refreshPressure() {

		if (pressure != null) {
			checkdate_tv.setText(pressure.getCheckdate());
			functioncode_tv.setText(pressure.getFunctioncode());
			cardNo_tv.setText(pressure.getCardNo());
			sbp_p_tv.setText(pressure.getSbp());
			dbp_p_tv.setText(pressure.getDbp());
			pulse_p_tv.setText(pressure.getPulse());
			icpcode_tv.setText(pressure.getIcpcode());
		}
	}
}
